home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1995 April / Internet Tools.iso / osi / isode / dosisode / DOSISODE80.ZIP / ISODE8.WRK / UNIX / LIB / DNS / RES_COMP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-16  |  7.5 KB  |  331 lines

  1. #include <fiddle.h>
  2. /*
  3.  * Copyright (c) 1985 Regents of the University of California.
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms are permitted provided
  7.  * that: (1) source distributions retain this entire copyright notice and
  8.  * comment, and (2) distributions including binaries display the following
  9.  * acknowledgement:  ``This product includes software developed by the
  10.  * University of California, Berkeley and its contributors'' in the
  11.  * documentation or other materials provided with the distribution and in
  12.  * all advertising materials mentioning features or use of this software.
  13.  * Neither the name of the University nor the names of its contributors may
  14.  * be used to endorse or promote products derived from this software without
  15.  * specific prior written permission.
  16.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  17.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  18.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  19.  */
  20.  
  21. #if defined(LIBC_SCCS) && !defined(lint)
  22. static char sccsid[] = "@(#)res_comp.c    6.18 (Berkeley) 6/27/90";
  23. #endif /* LIBC_SCCS and not lint */
  24.  
  25. #include <sys/types.h>
  26. #include <stdio.h>
  27. #include <arpa/nameser.h>
  28.  
  29. static dn_find();
  30.  
  31. /*
  32.  * Expand compressed domain name 'comp_dn' to full domain name.
  33.  * 'msg' is a pointer to the begining of the message,
  34.  * 'eomorig' points to the first location after the message,
  35.  * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
  36.  * Return size of compressed name or -1 if there was an error.
  37.  */
  38. dn_expand(msg, eomorig, comp_dn, exp_dn, length)
  39.     u_char *msg, *eomorig, *comp_dn, *exp_dn;
  40.     int length;
  41. {
  42.     register u_char *cp, *dn;
  43.     register int n, c;
  44.     u_char *eom;
  45.     int len = -1, checked = 0;
  46.  
  47.     dn = exp_dn;
  48.     cp = comp_dn;
  49.     eom = exp_dn + length;
  50.     /*
  51.      * fetch next label in domain name
  52.      */
  53.     while (n = *cp++) {
  54.         /*
  55.          * Check for indirection
  56.          */
  57.         switch (n & INDIR_MASK) {
  58.         case 0:
  59.             if (dn != exp_dn) {
  60.                 if (dn >= eom)
  61.                     return (-1);
  62.                 *dn++ = '.';
  63.             }
  64.             if (dn+n >= eom)
  65.                 return (-1);
  66.             checked += n + 1;
  67.             while (--n >= 0) {
  68.                 if ((c = *cp++) == '.') {
  69.                     if (dn + n + 2 >= eom)
  70.                         return (-1);
  71.                     *dn++ = '\\';
  72.                 }
  73.                 *dn++ = c;
  74.                 if (cp >= eomorig)    /* out of range */
  75.                     return(-1);
  76.             }
  77.             break;
  78.  
  79.         case INDIR_MASK:
  80.             if (len < 0)
  81.                 len = cp - comp_dn + 1;
  82.             cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff));
  83.             if (cp < msg || cp >= eomorig)    /* out of range */
  84.                 return(-1);
  85.             checked += 2;
  86.             /*
  87.              * Check for loops in the compressed name;
  88.              * if we've looked at the whole message,
  89.              * there must be a loop.
  90.              */
  91.             if (checked >= eomorig - msg)
  92.                 return (-1);
  93.             break;
  94.  
  95.         default:
  96.             return (-1);            /* flag error */
  97.         }
  98.     }
  99.     *dn = '\0';
  100.     if (len < 0)
  101.         len = cp - comp_dn;
  102.     return (len);
  103. }
  104.  
  105. /*
  106.  * Compress domain name 'exp_dn' into 'comp_dn'.
  107.  * Return the size of the compressed name or -1.
  108.  * 'length' is the size of the array pointed to by 'comp_dn'.
  109.  * 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0]
  110.  * is a pointer to the beginning of the message. The list ends with NULL.
  111.  * 'lastdnptr' is a pointer to the end of the arrary pointed to
  112.  * by 'dnptrs'. Side effect is to update the list of pointers for
  113.  * labels inserted into the message as we compress the name.
  114.  * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
  115.  * is NULL, we don't update the list.
  116.  */
  117. dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr)
  118.     u_char *exp_dn, *comp_dn;
  119.     int length;
  120.     u_char **dnptrs, **lastdnptr;
  121. {
  122.     register u_char *cp, *dn;
  123.     register int c, l;
  124.     u_char **cpp, **lpp, *sp, *eob;
  125.     u_char *msg;
  126.  
  127.     dn = exp_dn;
  128.     cp = comp_dn;
  129.     eob = cp + length;
  130.     if (dnptrs != NULL) {
  131.         if ((msg = *dnptrs++) != NULL) {
  132.             for (cpp = dnptrs; *cpp != NULL; cpp++)
  133.                 ;
  134.             lpp = cpp;    /* end of list to search */
  135.         }
  136.     } else
  137.         msg = NULL;
  138.     for (c = *dn++; c != '\0'; ) {
  139.         /* look to see if we can use pointers */
  140.         if (msg != NULL) {
  141.             if ((l = dn_find(dn-1, msg, dnptrs, lpp)) >= 0) {
  142.                 if (cp+1 >= eob)
  143.                     return (-1);
  144.                 *cp++ = (l >> 8) | INDIR_MASK;
  145.                 *cp++ = l % 256;
  146.                 return (cp - comp_dn);
  147.             }
  148.             /* not found, save it */
  149.             if (lastdnptr != NULL && cpp < lastdnptr-1) {
  150.                 *cpp++ = cp;
  151.                 *cpp = NULL;
  152.             }
  153.         }
  154.         sp = cp++;    /* save ptr to length byte */
  155.         do {
  156.             if (c == '.') {
  157.                 c = *dn++;
  158.                 break;
  159.             }
  160.             if (c == '\\') {
  161.                 if ((c = *dn++) == '\0')
  162.                     break;
  163.             }
  164.             if (cp >= eob) {
  165.                 if (msg != NULL)
  166.                     *lpp = NULL;
  167.                 return (-1);
  168.             }
  169.             *cp++ = c;
  170.         } while ((c = *dn++) != '\0');
  171.         /* catch trailing '.'s but not '..' */
  172.         if ((l = cp - sp - 1) == 0 && c == '\0') {
  173.             cp--;
  174.             break;
  175.         }
  176.         if (l <= 0 || l > MAXLABEL) {
  177.             if (msg != NULL)
  178.                 *lpp = NULL;
  179.             return (-1);
  180.         }
  181.         *sp = l;
  182.     }
  183.     if (cp >= eob) {
  184.         if (msg != NULL)
  185.             *lpp = NULL;
  186.         return (-1);
  187.     }
  188.     *cp++ = '\0';
  189.     return (cp - comp_dn);
  190. }
  191.  
  192. /*
  193.  * Skip over a compressed domain name. Return the size or -1.
  194.  */
  195. dn_skipname(comp_dn, eom)
  196.     u_char *comp_dn, *eom;
  197. {
  198.     register u_char *cp;
  199.     register int n;
  200.  
  201.     cp = comp_dn;
  202.     while (cp < eom && (n = *cp++)) {
  203.         /*
  204.          * check for indirection
  205.          */
  206.         switch (n & INDIR_MASK) {
  207.         case 0:        /* normal case, n == len */
  208.             cp += n;
  209.             continue;
  210.         default:    /* illegal type */
  211.             return (-1);
  212.         case INDIR_MASK:    /* indirection */
  213.             cp++;
  214.         }
  215.         break;
  216.     }
  217.     return (cp - comp_dn);
  218. }
  219.  
  220. /*
  221.  * Search for expanded name from a list of previously compressed names.
  222.  * Return the offset from msg if found or -1.
  223.  * dnptrs is the pointer to the first name on the list,
  224.  * not the pointer to the start of the message.
  225.  */
  226. static
  227. dn_find(exp_dn, msg, dnptrs, lastdnptr)
  228.     u_char *exp_dn, *msg;
  229.     u_char **dnptrs, **lastdnptr;
  230. {
  231.     register u_char *dn, *cp, **cpp;
  232.     register int n;
  233.     u_char *sp;
  234.  
  235.     for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
  236.         dn = exp_dn;
  237.         sp = cp = *cpp;
  238.         while (n = *cp++) {
  239.             /*
  240.              * check for indirection
  241.              */
  242.             switch (n & INDIR_MASK) {
  243.             case 0:        /* normal case, n == len */
  244.                 while (--n >= 0) {
  245.                     if (*dn == '.')
  246.                         goto next;
  247.                     if (*dn == '\\')
  248.                         dn++;
  249.                     if (*dn++ != *cp++)
  250.                         goto next;
  251.                 }
  252.                 if ((n = *dn++) == '\0' && *cp == '\0')
  253.                     return (sp - msg);
  254.                 if (n == '.')
  255.                     continue;
  256.                 goto next;
  257.  
  258.             default:    /* illegal type */
  259.                 return (-1);
  260.  
  261.             case INDIR_MASK:    /* indirection */
  262.                 cp = msg + (((n & 0x3f) << 8) | *cp);
  263.             }
  264.         }
  265.         if (*dn == '\0')
  266.             return (sp - msg);
  267.     next:    ;
  268.     }
  269.     return (-1);
  270. }
  271.  
  272. /*
  273.  * Routines to insert/extract short/long's. Must account for byte
  274.  * order and non-alignment problems. This code at least has the
  275.  * advantage of being portable.
  276.  *
  277.  * used by sendmail.
  278.  */
  279.  
  280. u_short
  281. _getshort(msgp)
  282.     u_char *msgp;
  283. {
  284.     register u_char *p = (u_char *) msgp;
  285. #ifdef vax
  286.     /*
  287.      * vax compiler doesn't put shorts in registers
  288.      */
  289.     register u_long u;
  290. #else
  291.     register u_short u;
  292. #endif
  293.  
  294.     u = *p++ << 8;
  295.     return ((u_short)(u | *p));
  296. }
  297.  
  298. u_long
  299. _getlong(msgp)
  300.     u_char *msgp;
  301. {
  302.     register u_char *p = (u_char *) msgp;
  303.     register u_long u;
  304.  
  305.     u = *p++; u <<= 8;
  306.     u |= *p++; u <<= 8;
  307.     u |= *p++; u <<= 8;
  308.     return (u | *p);
  309. }
  310.  
  311.  
  312. putshort(s, msgp)
  313.     register u_short s;
  314.     register u_char *msgp;
  315. {
  316.  
  317.     msgp[1] = s;
  318.     msgp[0] = s >> 8;
  319. }
  320.  
  321. putlong(l, msgp)
  322.     register u_long l;
  323.     register u_char *msgp;
  324. {
  325.  
  326.     msgp[3] = l;
  327.     msgp[2] = (l >>= 8);
  328.     msgp[1] = (l >>= 8);
  329.     msgp[0] = l >> 8;
  330. }
  331.